﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XDerive.XStudio.SystemFramework;
using XDerive.XStudio.Support.SupportAppI.User;
using XDerive.XStudio.Common.CommonAppI;
using System.Diagnostics;

namespace WebStudio.Mvc.Core.Base
{
    /// <summary>
    /// Controller基类
    /// </summary>
    public class BaseController : Controller
    {


        #region 重载Controller 执行函数
        /// <summary>
        /// 重载Controller 执行函数
        /// </summary>
        /// <param name="requestContext"></param>
        protected override void Execute(System.Web.Routing.RequestContext requestContext)
        {
            //获取版本号
            ViewData["Master-VerNo"] = "1.00.2010.0101";

            //获取站点名称
            ViewData["Master-SiteName"] = XDerive.XStudio.Common.CommonAppI.HashParameterAppI.GetValue("SysConfig", "SystemName", true);

            //获取站点版权信息
            ViewData["Master-Copyright"] = XDerive.XStudio.Common.CommonAppI.HashParameterAppI.GetValue("SysConfig", "SystemInfo", true);

            base.Execute(requestContext);
        }
        #endregion

        #region 重载OnActionExecuted
        /// <summary>
        /// 重载OnActionExecuted
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ViewData["Master-IsAjaxRequest"] = Request.IsAjaxRequest();

            base.OnActionExecuted(filterContext);
        }
        #endregion


        //属性

        #region 获取当前用户信息
        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        public UserInfo CurrentUser
        {
            get
            {
                return UserAppI.CurrentUser;
            }
        }
        #endregion


        #region 默认组织机构目录属性
        /// <summary>
        /// 默认组织机构目录属性
        /// </summary>
        public int DefaultInstitutionDirectory
        {
            get
            {
                //return CurrentUser.InstitutionDirectoryCodeID;
                return SysFunction.IntParse(HashParameterAppI.GetValue("SysConfig", "DefaultInstitutionDirectory", true));
            }
        }
        #endregion

        #region 默认组织机构目录名称属性
        /// <summary>
        /// 默认组织机构目录名称属性
        /// </summary>
        public string DefaultInstitutionDirectoryName
        {
            get
            {
                return SysFunction.StringParse(DictionaryAppI.GetScalar("Sys_InstitutionDirectoryTB", "InstitutionDirectoryName", "InstitutionDirectoryCodeID = " + DefaultInstitutionDirectory, true));
            }
        }
        #endregion

        #region 默认组织机构目录简称属性
        /// <summary>
        /// 默认组织机构目录简称属性
        /// </summary>
        public string DefaultInstitutionDirectoryShortName
        {
            get
            {
                return SysFunction.StringParse(DictionaryAppI.GetScalar("Sys_InstitutionDirectoryTB", "InstitutionDirectoryShortName", "InstitutionDirectoryCodeID = " + DefaultInstitutionDirectory, true));
            }
        }
        #endregion


        #region 当前动作名称
        /// <summary>
        /// 当前动作名称，用于呈现工具栏高亮
        /// </summary>
        public string CurrentAction
        {
            set
            {
                ViewData["CurrentAction"] = value;
            }
        }
        #endregion

        #region 当前Controller名称
        /// <summary>
        /// 当前Controller名称，用于呈现工具栏高亮
        /// </summary>
        public string CurrentController
        {
            set
            {
                ViewData["CurrentController"] = value;
            }
        }
        #endregion

        #region 当前页面标题
        /// <summary>
        /// 当前页面标题
        /// </summary>
        public string PageTitle
        {
            set
            {
                ViewData["PageTitle"] = value;
            }
        }
        #endregion


        #region 页面大小属性
        /// <summary>
        /// 页面大小属性
        /// </summary>
        public int PageSize
        {
            get
            {
                if (Request.Cookies["PageSize"] == null)
                {
                    Response.Cookies["PageSize"].Value = "20";
                    Response.Cookies["PageSize"].Expires = DateTime.MaxValue;
                    return 20;
                }
                else if (ViewData["PageSize"] != null)
                {
                    return SysFunction.IntParse(ViewData["PageSize"]);
                }
                else
                {
                    return SysFunction.IntParse(Request.Cookies["PageSize"].Value);
                }
            }
            set
            {
                ViewData["PageSize"] = value.ToString();
                Response.Cookies["PageSize"].Value = value.ToString();
                Response.Cookies["PageSize"].Expires = DateTime.MaxValue;
            }
        }
        #endregion

        #region [否决]页码属性
        /// <summary>
        /// [否决]页码属性
        /// </summary>
        public int PageNo
        {
            get
            {
                int pageNo = 0;
                if (Request["PageNo"] != null)
                {
                    pageNo = SysFunction.IntParse(Request["PageNo"]);
                }
                else if (ViewData["PageNo"] != null)
                {
                    pageNo = SysFunction.IntParse(ViewData["PageNo"]);
                }
                if (pageNo <= 0)
                {
                    pageNo = 1;
                }
                return pageNo;
            }
        }
        #endregion

        #region 页索引属性
        /// <summary>
        /// 页索引属性(从0开始)
        /// </summary>
        public int PageIndex
        {
            get
            {
                int pageIndex = 0;
                if (Request["PageNo"] != null)
                {
                    pageIndex = SysFunction.IntParse(Request["PageNo"]) - 1;
                }
                else if (ViewData["PageNo"] != null)
                {
                    pageIndex = SysFunction.IntParse(ViewData["PageNo"]) - 1;
                }
                if (pageIndex < 0)
                {
                    pageIndex = 0;
                }
                return pageIndex;
            }
        }
        #endregion

        #region 记录总数属性
        /// <summary>
        /// 记录总数属性
        /// </summary>
        public int RecordCount
        {
            get
            {
                return SysFunction.IntParse(ViewData["RecordCount"]);
            }
            set
            {
                ViewData["RecordCount"] = value;
            }
        }
        #endregion

        #region 命令字
        /// <summary>
        /// 命令字
        /// </summary>
        public string CommandCode
        {
            get
            {
                return SysFunction.StringParse(ViewData["CommandCode"]);
            }
            set
            {
                ViewData["CommandCode"] = value;
            }
        }
        #endregion

    }
}
